package com.hospital.phyexam.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.phyexam.dao.AdminOfficeMapper;
import com.hospital.phyexam.dao.PhyexamProjectMapper;
import com.hospital.phyexam.dao.RelationPhyBatchMapper;
import com.hospital.phyexam.entity.PhyexamProject;
import com.hospital.phyexam.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl implements ProjectService {

    @Autowired
    PhyexamProjectMapper projectMapper;
    @Autowired
    AdminOfficeMapper adminOfficeMapper;
    @Autowired
    RelationPhyBatchMapper relationMapper;


    public Map getAll(PhyexamProject phyexamProject){
    Map<String,String>map=new HashMap<>();
    map.put("id",phyexamProject.getId().toString());
    map.put("proName",phyexamProject.getProName());
    map.put("ofcName",adminOfficeMapper.selectByPrimaryKey(phyexamProject.getProOfcId()).getOfcName());
    map.put("proNorm",phyexamProject.getProNorm());
return map;
}

    @Override
    public String getAllProject(){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        List<PhyexamProject> phyexamProjects=projectMapper.selectAllPro();
        for (PhyexamProject phyexamProject:phyexamProjects){
            resaultmap.add(getAll(phyexamProject));
        }
        return JSON.toJSONString(resaultmap);
    }
    @Override
    public String selectByProName(String name){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        PhyexamProject phyexamProject=projectMapper.selectByProName(name);
        resaultmap.add(getAll(phyexamProject));
        return JSON.toJSONString(resaultmap);
    }

    @Override
    public String addPro(int adminId,String proName,String proNorm){
        PhyexamProject phyexamProject=new PhyexamProject();
        phyexamProject.setProName(proName);
        phyexamProject.setProNorm(proNorm);
        phyexamProject.setProOfcId(adminId);
        projectMapper.insert(phyexamProject);
        return getAllProject();
    }
    @Override
    public String update(int id,String name,String norm){
        PhyexamProject phyexamProject=projectMapper.selectByPrimaryKey(id);
        phyexamProject.setProNorm(norm);
        phyexamProject.setProName(name);
        projectMapper.updateByPrimaryKey(phyexamProject);
        return getAllProject();
    }
    @Override
    public String delete(int id){
        relationMapper.deleteByProId(id);
        projectMapper.deleteByPrimaryKey(id);

        return getAllProject();
    }

}
